package com.example.jh352160.surfingtv.starsight;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;

import com.example.jh352160.surfingtv.R;
import com.example.jh352160.surfingtv.network.item.StarsightItem;
import com.example.jh352160.surfingtv.network.retrofit.CommonRetrofit;
import com.example.jh352160.surfingtv.network.service.StarsightService;
import com.example.jh352160.surfingtv.starsight.customView.StarsightImage;
import com.example.jh352160.surfingtv.starsight.customView.adapter.StarsightAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

/**
 * Created by jh352160 on 2016/7/21
 */

public class StarsightMain extends Fragment implements View.OnClickListener {

    public static StarsightMain newInstance() {
        return new StarsightMain();
    }

    public static final int NETWORK_OK = 0;
    private ImageView category_image1, category_image2, category_image3, category_image4, category_image5;
    private ImageView iv1, iv2, iv3, iv4, iv5;
    public Map<String, int[]> map;
    private List<View> viewList;
    private ListView imageList;
    private View view;
    Retrofit retrofit;
    StarsightService starsightService;
    String[] images;
    List<StarsightItem.DataBean.ItemsBean> itemsBeanlist;

    public interface FragmentListener {
        void toLogin();
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case NETWORK_OK:
                    setimage();
            }
        }
    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        view = inflater.inflate(R.layout.starsight_page, container, false);

        init();

        //api
        retrofit = CommonRetrofit.getRetrofit();
        starsightService = retrofit.create(StarsightService.class);

        category_image1.performClick();

        return view;
    }

    private void init() {
        category_image1 = (ImageView) view.findViewById(R.id.textView);
        category_image2 = (ImageView) view.findViewById(R.id.textView2);
        category_image3 = (ImageView) view.findViewById(R.id.textView3);
        category_image4 = (ImageView) view.findViewById(R.id.textView4);
        category_image5 = (ImageView) view.findViewById(R.id.textView5);
        category_image1.setOnClickListener(this);
        category_image2.setOnClickListener(this);
        category_image3.setOnClickListener(this);
        category_image4.setOnClickListener(this);
        category_image5.setOnClickListener(this);
        iv1 = (ImageView) view.findViewById(R.id.imageView1);
        iv2 = (ImageView) view.findViewById(R.id.imageView2);
        iv3 = (ImageView) view.findViewById(R.id.imageView3);
        iv4 = (ImageView) view.findViewById(R.id.imageView4);
        iv5 = (ImageView) view.findViewById(R.id.imageView5);
        imageList = (ListView) view.findViewById(R.id.imageList);
        viewList = new ArrayList<>();
        int[] allStar = {R.drawable.starsight_anne_hathaway, R.drawable.starsight_big_image,
                R.drawable.starsight_big_image2, R.drawable.starsight_big_image3,
                R.drawable.starsight_big_image4, R.drawable.starsight_big_image5};
        int[] star1 = {R.drawable.starsight_anne_hathaway};
        int[] star2 = {R.drawable.starsight_big_image2};
        int[] star3 = {R.drawable.starsight_big_image3};
        int[] star4 = {R.drawable.starsight_big_image4};

        map = new HashMap<>();
        map.put("all", allStar);
        map.put("star1", star1);
        map.put("star2", star2);
        map.put("star3", star3);
        map.put("star4", star4);

    }

    @Override
    public void onClick(View view) {
        viewList = new ArrayList<>();
        iv1.setVisibility(View.INVISIBLE);
        iv2.setVisibility(View.INVISIBLE);
        iv3.setVisibility(View.INVISIBLE);
        iv4.setVisibility(View.INVISIBLE);
        iv5.setVisibility(View.INVISIBLE);
        category_image1.setImageResource(R.drawable.starsight_allstar_unselect);
        category_image2.setImageResource(R.drawable.starsight_foreign_male_unselect);
        category_image3.setImageResource(R.drawable.starsight_domestic_male_unselect);
        category_image4.setImageResource(R.drawable.starsight_domestic_female_unselect);
        category_image5.setImageResource(R.drawable.starsight_foreign_female_unselect);
        switch (view.getId()) {
            case R.id.textView:
                getImageUrl(0);
                iv1.setVisibility(View.VISIBLE);
                category_image1.setImageResource(R.drawable.starsight_allstar_select);
                break;
            case R.id.textView2:
                getImageUrl(1);
                iv2.setVisibility(View.VISIBLE);
                category_image2.setImageResource(R.drawable.starsight_foreign_male_select);
                break;
            case R.id.textView3:
                getImageUrl(3);
                iv3.setVisibility(View.VISIBLE);
                category_image3.setImageResource(R.drawable.starsight_domestic_male_select);
                break;
            case R.id.textView4:
                getImageUrl(2);
                iv4.setVisibility(View.VISIBLE);
                category_image4.setImageResource(R.drawable.starsight_domestic_female_select);
                break;
            case R.id.textView5:
                getImageUrl(4);
                iv5.setVisibility(View.VISIBLE);
                category_image5.setImageResource(R.drawable.starsight_foreign_female_select);
                break;
        }
    }

    private void getImageUrl(int type) {
        Call<StarsightItem> call = starsightService.getItem(type);
        call.enqueue(new Callback<StarsightItem>() {
            @Override
            public void onResponse(Call<StarsightItem> call, Response<StarsightItem> response) {
                if (response.body().getCode().equals("200")) {
                    StarsightItem.DataBean dataBean = response.body().getData();
                    images = new String[dataBean.getTotal()];
                    itemsBeanlist = dataBean.getItems();
                    for (int i = 0; i < dataBean.getItems().size(); i++) {
                        images[i] = dataBean.getItems().get(i).getImageUrl();
                    }
                    Message message = new Message();
                    message.what = NETWORK_OK;
                    handler.sendMessage(message);
                }
            }

            @Override
            public void onFailure(Call<StarsightItem> call, Throwable t) {
                Log.e("Retrofit Error", t.toString());
            }
        });
    }

    private void setimage() {
        for (StarsightItem.DataBean.ItemsBean i : itemsBeanlist) {
            StarsightImage starsightImage = new StarsightImage(this.getActivity(), i);
            starsightImage.setImage(i.getImageUrl(), i.getFavoriteCount(), i.getFavorite(), i.getContentId(), i.getDetailUrl(), i.getTitle());
            viewList.add(starsightImage);
        }
        StarsightAdapter adapter = new StarsightAdapter(this.getActivity(), android.R.layout.simple_expandable_list_item_1, viewList);
        imageList.setAdapter(adapter);
    }

}
